package com.halobits.thenumbergames;

import android.content.Context;
import android.view.Gravity;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

/**
 * Created by RAFAEL.SUENO on 1/14/2015.
 */
public class GameBoardHelper {

  protected LinearLayout ll;
  protected LinearLayout.LayoutParams llp;
  protected int[] board;
  protected int gridSize;
  protected GameButton[] buttonCollection;
  protected int boardSize;

  public GameBoardHelper(Context context, int gridSize, int[] board) {

    this.gridSize = gridSize;
    this.board = board;
    this.boardSize = gridSize*gridSize;

    buttonCollection = new GameButton[boardSize];

    llp = new LinearLayout.LayoutParams(
        LinearLayout.LayoutParams.WRAP_CONTENT,
        LinearLayout.LayoutParams.WRAP_CONTENT
    );

    ll = new LinearLayout(context);
    ll.setHorizontalGravity(Gravity.CENTER_HORIZONTAL);
    ll.setOrientation(LinearLayout.VERTICAL);

    for(int i = 0; i < boardSize; i++) {
      buttonCollection[i] = new GameButton(context);
    }

  }

  public void generateLayout(Context context) {
    int i = 0;
    for(int y = 0; y < this.gridSize; y++) {
      LinearLayout lltmp = new LinearLayout(context);

      LinearLayout.LayoutParams llptmp = new LinearLayout.LayoutParams(
          LinearLayout.LayoutParams.WRAP_CONTENT,
          LinearLayout.LayoutParams.WRAP_CONTENT
      );

      lltmp.setOrientation(LinearLayout.HORIZONTAL);

      for(int x = 0; x < this.gridSize; x++) {
        buttonCollection[i].setText(String.valueOf(board[i]));
        buttonCollection[i].setLayoutParams(llptmp);
        buttonCollection[i].setLocation(i);
        lltmp.addView(buttonCollection[i]);
        i++;
      }
      lltmp.setLayoutParams(llp);
      ll.addView(lltmp);
    }
  }

  public void setBoard(int[] board) {
    this.board = board;
  }

  public LinearLayout getLinearLayout() {
    return ll;
  }

  public LinearLayout.LayoutParams getLayoutParams() {
    return llp;
  }

  public GameButton[] getButtonCollection() {
    return buttonCollection;
  }
}
